package com.zbkj.admin.controller.brand;

import com.zbkj.common.annotation.LogControllerAnnotation;
import com.zbkj.common.enums.MethodType;
import com.zbkj.common.model.product.ProductCategory;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.request.merchant.MerchantSearchRequest;
import com.zbkj.common.response.*;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 平台商品库 前端控制器
 +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/brand/product/library")
@Api(tags = "平台商品库控制器") //配合swagger使用

public class BrandProductLibraryController {

    @Autowired
    private ProductLibraryService productLibraryService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private ProductManagerService productService;

    @Autowired
    private BrandService brandService;

    /**
     * 加入商品库
     * @param request 新增参数
     * @author dazongzi
     * @since 2025-06-05
     */
    @LogControllerAnnotation(intoDB = true, methodType = MethodType.ADD, description = "加入商品库")
//    @PreAuthorize("hasAuthority('brand:product:brand:add')")
    @ApiOperation(value = "加入商品库")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult<ProductLibraryIdResponse> add(@RequestBody @Validated ProductLibraryRequest request) {
        return CommonResult.success(productLibraryService.add(request));
    }

    @LogControllerAnnotation(intoDB = true, methodType = MethodType.ADD, description = "批量加入商品库")
//    @PreAuthorize("hasAuthority('brand:product:brand:add')")
    @ApiOperation(value = "批量加入商品库")
    @RequestMapping(value = "/batch/add", method = RequestMethod.POST)
    public CommonResult<String> batchAdd(@RequestBody @Validated ProductLibraryBatchAddRequest request) {
        if (productLibraryService.batchAdd(request)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除平台商品库
     * @param id Integer
     * @author dazongzi
     * @since 2025-06-05
     */
    @ApiOperation(value = "删除商品库")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        if(productLibraryService.removeById(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 商品分类列表
     * 无层级，前端处理层级
     * @return ProductCategory 商品分类
     * @throws Exception
     */
//    @PreAuthorize("hasAuthority('brand:merchant:page:list')")
    @ApiOperation(value = "商品分类列表")
    @RequestMapping(value = "/category/list", method = RequestMethod.GET)
    public CommonResult<List<ProductCategory>> getList() throws Exception {
        return CommonResult.success(productCategoryService.getProductLibraryProductCategoryListBrand());
    }

    @ApiOperation(value = "品牌商分页列表")
    @RequestMapping(value = "/brand/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", dataType = "string"),
    })
    public CommonResult<CommonPage<BrandResponse>> getBrandPageList(@Validated BrandSearchRequest request,
                                                           @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(brandService.getSelectedBrandListBrand(request, pageParamRequest)));
    }

//    @PreAuthorize("hasAuthority('brand:merchant:page:list')")
    @ApiOperation(value="商户分页列表")
    @RequestMapping(value = "/merchant/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MerchantPageResponse>> getMerchantPageList(@Validated MerchantSearchRequest searchRequest,
                                                                      @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(merchantService.getProductLibraryMerchantListBrand(searchRequest, pageParamRequest)));
    }

//    @PreAuthorize("hasAuthority('brand:product:page:list')")
    @ApiOperation(value = "商品分页列表") //配合swagger使用
    @RequestMapping(value = "/product/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<PlatformProductListResponse>> getProductPageList(@Validated ProductLibraryListSearchRequest request,
                                                                         @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(productService.getProductLibraryProductListBrand(request, pageParamRequest)));
    }

    /**
     * 分页显示已选分类列表
     * @param request 搜索条件
     * @param pageParamRequest 分页参数
     * @author dazongzi
     * @since 2025-06-05
     */
    @ApiOperation(value = "已选分类列表(下拉选择)") //配合swagger使用
    @RequestMapping(value = "/selected/category/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<ProductLibrarySelectedCategoryResponse>> getSelectedCategoryList(@Validated ProductLibrarySearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<ProductLibrarySelectedCategoryResponse> productLibraryCommonPage = CommonPage.restPage(productLibraryService.getSelectedCategoryList(request, pageParamRequest));
        return CommonResult.success(productLibraryCommonPage);
    }

    /**
     * 分页显示已选商户列表
     * @param request 搜索条件
     * @param pageParamRequest 分页参数
     * @author dazongzi
     * @since 2025-06-05
     */
    @ApiOperation(value = "已选商户列表(下拉选择)") //配合swagger使用
    @RequestMapping(value = "/selected/merchant/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<ProductLibrarySelectedMerchantResponse>> getSelectedMerchantList(@Validated ProductLibrarySearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<ProductLibrarySelectedMerchantResponse> productLibraryCommonPage = CommonPage.restPage(productLibraryService.getSelectedMerchantList(request, pageParamRequest));
        return CommonResult.success(productLibraryCommonPage);
    }

    /**
     * 分页显示已选商品列表
     * @param request 搜索条件
     * @param pageParamRequest 分页参数
     * @author dazongzi
     * @since 2025-06-05
     */
    @ApiOperation(value = "已选商品列表(下拉选择)") //配合swagger使用
    @RequestMapping(value = "/selected/product/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<ProductLibrarySelectedProductResponse>> getSelectedProductList(@Validated ProductLibrarySearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<ProductLibrarySelectedProductResponse> productLibraryCommonPage = CommonPage.restPage(productLibraryService.getSelectedProductList(request, pageParamRequest));
        return CommonResult.success(productLibraryCommonPage);
    }

    @ApiOperation(value = "已选品牌商列表(下拉选择)") //配合swagger使用
    @RequestMapping(value = "/selected/brand/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<ProductLibrarySelectedBrandResponse>> getSelectedBrandList(@Validated ProductLibrarySearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<ProductLibrarySelectedBrandResponse> productLibraryCommonPage = CommonPage.restPage(productLibraryService.getSelectedBrandList(request, pageParamRequest));
        return CommonResult.success(productLibraryCommonPage);
    }

//    @PreAuthorize("hasAuthority('brand:product:activity:search:page')")
    @ApiOperation(value = "商品搜索分页列表(装修)")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "brandId", value = "品牌商ID，逗号分割，商品库中选择了品牌商ID时传入", dataType = "string"),
    })
    @RequestMapping(value = "/product/search/page", method = RequestMethod.GET)
    public CommonResult<CommonPage<ProductActivityResponse>> getActivitySearchPage(
            @Validated ProductLibraryProductSearchRequest request, @Validated PageParamRequest pageRequest) {
        return CommonResult.success(CommonPage.restPage(productService.getActivitySearchPageLibrary(request, pageRequest,null)));
    }

    @ApiOperation(value = "所有已选记录") //配合swagger使用
    @RequestMapping(value = "/all/selected/list", method = RequestMethod.GET)
    public CommonResult<List<Integer>> getAllSelectedList(@Validated ProductLibrarySearchRequest request, @Validated PageParamRequest pageParamRequest) {
        List<Integer> list = productLibraryService.getAllSelectedList(request, pageParamRequest);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "已选商品搜索分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandId", value = "品牌商ID，逗号分割，商品库中选择了品牌商ID时传入", dataType = "string"),
    })
    @RequestMapping(value = "/all/selected/page", method = RequestMethod.GET)
    public CommonResult<CommonPage<ProductActivityResponse>> getProductSearchPage(
            @Validated ProductLibraryProductSearchRequest request, @Validated PageParamRequest pageRequest) {
        return CommonResult.success(CommonPage.restPage(productService.getActivitySearchPageLibrary(request, pageRequest,null)));
    }

    @ApiOperation(value = "商品详情") //配合swagger使用
    @RequestMapping(value = "/product/{id}", method = RequestMethod.GET)
    public CommonResult<PlatformProductDetailResponse> getProductDetailList(@PathVariable(name = "id") Integer id) {
        return CommonResult.success(productService.getProductLibraryProductDetailBrand(id));
    }

}



